package com.fary.consumer;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
import org.springframework.messaging.converter.MessageConversionException;
import org.springframework.stereotype.Component;

@Component
@RabbitListener
public class RabbitMQListener implements ChannelAwareMessageListener {

    @Override
    @RabbitListener()
    public void onMessage(Message message, Channel channel) throws Exception {
        try {
            System.out.println("消息接收成功");
            System.out.println((new String(message.getBody())));
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
        } catch (Exception e) {
            System.out.println("消息接受失败");
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), true, true);
        }
        Thread.sleep(1000);
    }

}
